import pytest
from utils.driver_manager import DriverManager


@pytest.fixture(scope="session", autouse=True)
def driver_setup():
    """测试会话开始时初始化驱动，结束时退出驱动"""
    # 初始化驱动
    DriverManager.get_driver()
    yield

    # 测试结束后退出驱动
    DriverManager.quit_driver()


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """获取测试结果并在失败时截图"""
    outcome = yield
    rep = outcome.get_result()

#
    if rep.when == "call" and rep.failed:
        driver = DriverManager.get_driver()
        screenshot_name = f"../reports/pngs/screenshot_{item.name}_{rep.when}.png"
        driver.save_screenshot(screenshot_name)
        print(f"测试失败，已保存截图: {screenshot_name}")